package com.utils.v24;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.support.v4.content.FileProvider;

import com.gxpt.app.Constant;
import com.utils.logger.Logger;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by dufeng on 2017/6/23.
 *
 * http://blog.csdn.net/u011150924/article/details/71748464?locationNum=4&fps=1
 *
 * https://yq.aliyun.com/articles/59477
 */

public class v_2_4 {

    public static Uri get24MediaFileUri(Context context) {
        Uri uri;
//        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Constant.PRO_DIR);
//        if (!mediaStorageDir.exists()) {
//            if (!mediaStorageDir.mkdirs()) {
//                return null;
//            }
//        }

        String storagePath = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                .getAbsolutePath() + File.separator + Constant.PRO_DIR;

        File storageDir;
        storageDir = new File(storagePath);
        storageDir.mkdirs();
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

//        //创建Media File
        File mediaFile;
        mediaFile = new File(storagePath +"/"+ Constant.UPLOAD_PIC_TEMP_NAME);


        Logger.i(" = "+mediaFile.getPath());

        uri = FileProvider
                .getUriForFile(context.getApplicationContext(), "com.example.android.fileprovider", mediaFile);


        return uri;
    }

    public static Uri getMediaFileUri(Context context){
//        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), Constant.PRO_DIR);
//        if (!mediaStorageDir.exists()) {
//            if (!mediaStorageDir.mkdirs()) {
//                return null;
//            }
//        }


        String storagePath = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                .getAbsolutePath() + File.separator + Constant.PRO_DIR;

        File storageDir;
        storageDir = new File(storagePath);
        storageDir.mkdirs();
        if (!storageDir.exists()) {
            storageDir.mkdirs();
        }

//        //创建Media File
        File mediaFile;
        mediaFile = new File(storagePath +"/"+ Constant.UPLOAD_PIC_TEMP_NAME);


//        //创建Media File
//        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//        File mediaFile;
////        if (type == TYPE_TAKE_PHOTO) {
////            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
//            mediaFile = new File(mediaStorageDir.getPath() +"/"+ Constant.UPLOAD_PIC_TEMP_NAME);
////        } else {
////            return null;
////        }
        return Uri.fromFile(mediaFile);
    }
}
